package com.zts.modules.sys.service.impl;

import com.google.common.base.Strings;
import com.zts.base.exception.InternalServerErrorException;
import com.zts.base.exception.InvalidArgumentException;
import com.zts.base.page.PageBound;
import com.zts.modules.sys.entity.Code;
import com.zts.modules.sys.mapper.CodeMapper;
import com.zts.modules.sys.service.CodeService;
import com.zts.util.BeanUtil;
import com.zts.util.BeanValidatorsUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by lzk on 2017/6/16.
 */
@Service
public class CodeServiceImpl  implements CodeService {
    @Autowired
    CodeMapper codeMapper;

    @Override
    public Code get(Long id) {
        if(null == id || id < 0){
            throw new InvalidArgumentException("参数<id>为空");
        }
        return codeMapper.get(id);
    }

    @Override
    public List<Code> getByEntity(Code entity) {
        return null;
    }

    @Override
    public List<Code> getByCondition(Map<String, Object> conditions) {
        return null;
    }

    @Override
    public List<Code> getList(Code entity, PageBound pageBound) {
        if (null == entity) {
            entity = new Code();
        }
        return codeMapper.getList(entity, pageBound);
    }
    @Override
    @Transactional
    public Code insert(Code entity) {
        if (null == entity) {
            throw new InvalidArgumentException("参数不合法");
        }
        //先对entity进行校验
        BeanValidatorsUtil.validateWithException(entity);
        //校验通过后执行新增操作
        int i = codeMapper.insert(entity);
        if (i <= 0 || entity.getId() == null) {
            throw new InternalServerErrorException("新增失败");
        }
        return codeMapper.get(entity.getId());
    }

    @Override
    public int update(Code entity) {
        if(null == entity){
            throw new InvalidArgumentException("参数为空");
        }
        if(null == entity.getCataCode() || "".equals(entity.getCataCode())){
            throw new InvalidArgumentException("参数对象类别标识为空");
        }
        if(null == entity.getSubCode() || "".equals(entity.getSubCode())){
            throw new InvalidArgumentException("参数对象子类别标识为空");
        }
        Code old = codeMapper.getCode(entity.getCataCode(),entity.getSubCode());
        BeanUtils.copyProperties(entity, old, BeanUtil.getNullPropertyNames(entity));
        BeanValidatorsUtil.validateWithException(old);
        return codeMapper.update(old);
    }


    @Override
    public int delete(Code entity) {
        //用户id必须存在，否则抛出异常。
        if (null == entity) {
            throw new InvalidArgumentException("对象不能为空");
        } else if (null == entity.getId() || entity.getId() <= 0) {
            throw new InvalidArgumentException("必须传入id ");
        }
        return codeMapper.delete(entity);
    }

    @Override
    public Code save(Code entity) {
        return null;
    }

    @Override
    public int updateCodeByid(Code code) {
        if(null == code){
            throw new InvalidArgumentException("参数为空");
        }
        if(null == code.getId() || code.getId() < 0){
            throw new InvalidArgumentException("参数对象id为空");
        }
        Code old = codeMapper.get(code.getId() );
        BeanUtils.copyProperties(code, old, BeanUtil.getNullPropertyNames(code));
        BeanValidatorsUtil.validateWithException(old);
        return codeMapper.updateCodeByid(old);
    }

    @Override
    public int deleteCodeBycataCode(Code code) {
        //用户类别标识必须存在，否则抛出异常。
        if (null == code) {
            throw new InvalidArgumentException("对象不能为空");
        }if(null == code.getCataCode() || "".equals(code.getCataCode())){
            throw new InvalidArgumentException("参数对象类别标识为空");
        }
        return codeMapper.deleteCodeBycataCode(code);
    }

    @Override
    public int deleteCodeBycataAndSub(Code code) {
        //用户类别标识与子类别标识必须存在，否则抛出异常。
        if (null == code) {
            throw new InvalidArgumentException("对象不能为空");
        }if(Strings.isNullOrEmpty(code.getCataCode())){
            throw new InvalidArgumentException("参数对象类别标识为空");
        }
        return codeMapper.deleteCodeBycataAndSub(code);
    }
}
